﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Data;
namespace BenefitHelper.Sys
{
    /// <summary>
    /// 基金管理员表(需要)
    /// </summary>
    public class Operator
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 登录名
        /// </summary>
        [Display(Name = "登录名"), Required(ErrorMessage = "登录名不能为空")]
        public string Username { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        [Display(Name = "密码"), Required(ErrorMessage = "密码不能为空")]
        public string Password { get; set; }
        /// <summary>
        /// 昵称
        /// </summary>
        [Display(Name = "姓名"), Required(ErrorMessage = "昵称不能为空")]
        public string UName { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        [Display(Name = "性别")]
        public string Sex { get; set; }

        /// <summary>
        /// 进公司批次
        /// </summary>
        [Display(Name = "进公司批次")]
        public int BatchId { get; set; }
        /// <summary>
        /// 进公司批次
        /// </summary>
        [Display(Name = "进公司批次")]
        public Batch Batch { get; set; }
        /// <summary>
        /// 邮箱
        /// </summary>
        [Display(Name = "邮箱")]
        public string Email { get; set; }
        /// <summary>
        /// 电话
        /// </summary>
        [Display(Name = "电话")]
        public string Phone { get; set; }
        /// <summary>
        /// 是否被删除
        /// </summary>
        [Display(Name = "是否被删除")]
        public bool IsDeleted { get; set; }
        /// <summary>
        /// 删除时间
        /// </summary>
        [Display(Name = "删除时间")]
        DateTime DeletedTime { get; set; }

        /// <summary>
        /// 得到所有操作员
        /// </summary>
        /// <returns></returns>
        public List<Operator> GetList(DB.DBManager db)
        {
            var query = (from t in db.Operators select t).ToList();
            return query;
        }
        /// <summary>
        /// 得到所有没有没删除的操作员
        /// </summary>
        /// <returns></returns>
        public List<Operator> GetUnDeletedOperators(DB.DBManager db)
        {
            var query = (from t in db.Operators where t.IsDeleted == false select t).ToList();
            return query;
        }
        /// <summary>
        /// 增加一个交易员
        /// </summary>
        /// <returns>交易员编号</returns>
        public int AddOperator(DB.DBManager db)
        {
            Operator op = new Operator();
            Sys.Batch _batch = new Sys.Batch();
            op.BatchId = _batch.GetNewBatch();
            op.Email = this.Email;
            op.IsDeleted = false;
            op.Password = this.Password;
            op.Phone = this.Phone;
            op.Sex = this.Sex;
            op.UName = this.UName;
            op.Username = this.Username;
            db.Operators.Add(op);
            db.SaveChanges();

            int operatorid = op.Id;
            OperatorInfo info = new OperatorInfo();
            info.OperatorId = operatorid;
            info.AddOperatorInfo(db);

            return operatorid;
        }

        public Operator GetModel(int id, DB.DBManager db)
        {
            Operator op = null;
            var query = db.Operators.Find(id);
            if (query != null)
            {
                op = query;
            }
            return op;
        }
        /// <summary>
        /// 获取成长指数
        /// </summary>
        /// <param name="lastMonthProfit"></param>
        /// <param name="MonthProfit"></param>
        /// <returns></returns>
        public int GetGrowthIndex(double lastMonthProfit, double MonthProfit)
        {
            if (lastMonthProfit <= 0)
            {
                lastMonthProfit = 1;
            }
            if (MonthProfit <= 0)
            {
                MonthProfit = 1;
            }
            int index = Convert.ToInt32((MonthProfit - lastMonthProfit) / lastMonthProfit) * 100;
            return index;
        }
        /// <summary>
        /// 获取稳定指数
        /// </summary>
        /// <param name="lastMonthProfit"></param>
        /// <param name="MonthProfit"></param>
        /// <returns></returns>
        public int GetStableIndex(double lastMonthProfit, double MonthProfit)
        {
            if (lastMonthProfit <= 0)
            {
                lastMonthProfit = 1;
            }
            if (MonthProfit <= 0)
            {
                MonthProfit = 1;
            }
            lastMonthProfit = Math.Sqrt(lastMonthProfit);
            MonthProfit = Math.Sqrt(MonthProfit);
            int index = 0;
            if (lastMonthProfit > MonthProfit)
            {
                index = Convert.ToInt32(MonthProfit / lastMonthProfit) * 100;
            }
            else
            {
                index = Convert.ToInt32(lastMonthProfit / MonthProfit) * 100;
            }
            return index;
        }
        /// <summary>
        /// 获取指数的图片的数量
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public int GetPicCount(int index)
        {
            int returnindex = 0;
            if (index < 20)
            {
                returnindex = 1;
            }
            else if (index >= 20 && index < 40)
            {
                returnindex = 2;
            }
            else if (index >= 40 && index < 60)
            {
                returnindex = 3;
            }
            else if (index >= 60 && index < 80)
            {
                returnindex = 4;
            }
            else
            {
                returnindex = 5;
            }
            return returnindex;
        }
        /// <summary>
        /// 操作员登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public int OperatorLogin(string username, string password, int tradeHistoryId, string pdate, DB.DBManager db)
        {
            int operatorid = 0;
            var _query = db.Operators.Where(a => a.Username.Equals(username)).Where(a => a.Password.Equals(password));
            if (_query.Count() > 0)
            {
                operatorid = _query.First().Id;
                Common.Web.Cookie cookie = new Common.Web.Cookie();
                cookie.SetCookie("LoginUserId", operatorid.ToString(), 48);
                cookie.SetCookie("LoginUserType", Convert.ToInt32(Config.UserType.Operator).ToString(), 48);
                cookie.SetCookie("TradingDayId", tradeHistoryId.ToString(), 48);
                cookie.SetCookie("TradingDayPdate", pdate, 48);
            }
            return operatorid;
        }

        /// <summary>
        /// 得到同一个批次的所有交易员
        /// </summary>
        /// <param name="batch"></param>
        /// <returns></returns>
        public List<Operator> GetBatchOperators(int batch, DB.DBManager db)
        {
            var query = db.Operators.Where(a => a.BatchId == batch).Where(a => a.IsDeleted == false).ToList();
            return query;
        }

        /// <summary>
        /// 判断交易员当日是否有做交易
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public bool OperatorTradedAtDay(int operatorid, int tradeHistoryId, DB.DBManager db)
        {
            bool traded = false;

            List<View.OperatorDayProductInfo> operatorinfos = new View.OperatorDayProductInfo().GetOperatorDayProductInfo(tradeHistoryId, operatorid, db);
            if (operatorinfos.Count > 0)
            {
                traded = true;
                return traded;
            }

            return traded;
        }

        /// <summary>
        /// 判断交易员当日是否有做交易
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public bool OperatorTradedAtMonth(int operatorid, int month, int year, DB.DBManager db)
        {
            bool traded = false;

            List<View.OperatorDayProductInfo> operatorinfos = new View.OperatorDayProductInfo().GetOperatorMonthProductInfo(month, year, operatorid, db);
            if (operatorinfos.Count > 0)
            {
                traded = true;
                return traded;
            }

            return traded;
        }

    }
}